package exportword.test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder;

public class test {
	  private Configuration configuration = null;  
      
	    public test(){  
	        configuration = new Configuration();  
	        configuration.setDefaultEncoding("UTF-8");  
	    }  
	      
	    public static void main(String[] args) {  
	    	test test1 = new test();  
	        test1.createWord();  
	    }  
	      
	    public void createWord(){  
	        Map<String,Object> dataMap=new HashMap<String,Object>(); 
	        //基本信息
	        dataMap.put("name", "熊大");  
	        dataMap.put("idinfo", "身份证：420881198911072512"); 
	        dataMap.put("imgStr", getImageStr());
	        dataMap.put("createDate", new Date().toString());
	        //档案信息
	        List<XFile> files = new ArrayList<XFile>();
	        for (int i = 1; i < 10; i++) {
				XFile file = new XFile();
				file.setFileId("no" + i);
				file.setFileType("fileType" + i);
				file.setFileInfo("fileInfo" + i);
				
				files.add(file);
			}
	        if(files.size() > 0){
	        	dataMap.put("fileList", files);
	        }
	        //身份证信息
	        List<Card> cards = new ArrayList<Card>();
	        for (int i = 1; i < 10; i++) {
	        	Card card = new Card();
	        	card.setCardType("涉恐"+i);
	        	card.setCardInfo("打架" + i + "年");
				
	        	cards.add(card);
			}
	        if(cards.size() > 0){
	        	dataMap.put("cardList", cards);
	        } 
	        
	        List<Mobilephone> phones = new ArrayList<Mobilephone>();
	        for (int i = 1; i < 10; i++) {
	        	Mobilephone phone = new Mobilephone();
	        	phone.setNophone("" + i);
	        	phone.setPhoneNum("1311234567" + i);
	        	phone.setCompany("移动" + i);
	        	phone.setRelation("湖北" + i);
				
	        	phones.add(phone);
			}
	        List<Mobilephone> phones1 = new ArrayList<Mobilephone>();
	        for (int i = 1; i < 10; i++) {
	        	Mobilephone phone = new Mobilephone();
	        	phone.setNophone("" + i);
	        	phone.setPhoneNum("1311234567" + i);
	        	phone.setCompany("移动" + i);
	        	phone.setRelation("湖北" + i);
				
	        	phones1.add(phone);
			}
	        List<List<Mobilephone>> phoneLists = new ArrayList<List<Mobilephone>>();
	        if(phones.size() > 0){
	        	dataMap.put("phoneList", phoneLists);
	        } 
	        
	        
	        
//	        configuration.setClassForTemplateLoading(this.getClass(), "/com");  //FTL文件所存在的位置  
	        try {
				configuration.setDirectoryForTemplateLoading(new File("I:/"));
			} catch (IOException e2) {
				e2.printStackTrace();
			} 
	        Template t=null;  
	        try {
	            t = configuration.getTemplate("template.ftl"); //文件名
	        } catch (IOException e) {
	            e.printStackTrace();  
	        }
	        File outFile = new File("I:/test"+Math.random()*10000+".doc");  
	        Writer out = null;  
	        try {  
	            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));  
	        } catch (FileNotFoundException e1) {  
	            e1.printStackTrace();  
	        }  
	           
	        try {  
	            t.process(dataMap, out); 
	            System.out.println("===OK===");
	        } catch (TemplateException e) {  
	            e.printStackTrace();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        }  
	    }
	    
	    public String getImageStr() {  
            String imgFile = "I:/aa.png";  
            InputStream in = null;  
            byte[] data = null;  
            try {  
                in = new FileInputStream(imgFile);  
                data = new byte[in.available()];  
                in.read(data);  
                in.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            BASE64Encoder encoder = new BASE64Encoder();  
            return encoder.encode(data);  
        }
}
